// pages/theme/theme.js
const app = getApp()
Page({
     data: {
          pages:1,//页数
          id:"",//请求id
          types:"",//类型
          sorttype:0,//排序状态
          num:0,//导航动态标记
          yhj: '', //优惠券标记排序分式
          xl: '',//标记销量排序分式
          jg: '',//标记价格排序分式
          goodslist:[],//列表
     },
     onLoad: function (options) {
          var that=this
          that.setData({
               id: options.id,
               types: options.types,
               userid: wx.getStorageSync("userid")
          })
          // 判断标记排序分式
          if (this.data.yhj == "") {
               this.setData({
                    imageurlq: "/images/kong.png",
                    imageurl: "/images/kong.png",
                    imageurld: "/images/kong.png",
               })
          }
          that.request()
     },
     // 综合排序
     choosesorta: function (e) {
          var that = this
          that.setData({
               goodslist: [],
               num: e.currentTarget.dataset.num,
               imageurlq: "/images/kong.png",
               imageurl: "/images/kong.png",
               imageurld: "/images/kong.png",
               jg: '',
               yhj: '',
               yhj: '',
               sorttype: 0
          })
          that.request()
     },
     // 优惠券排序
     choosesortq: function (e) {
          var that = this
          that.setData({
               goodslist: [],
               num: e.currentTarget.dataset.num,
               imageurl: "/images/kong.png",
               imageurld: "/images/kong.png",
               jg: 0,
               xl: 0,
          })
          if (that.data.yhj == 0) {
               that.setData({
                    imageurlq: "/images/shang.png",
                    yhj: 1,
                    sorttype: 7
               })
          } else {
               that.setData({
                    imageurlq: "/images/xia.png",
                    yhj: 0,
                    sorttype: 8
               })
          }
          that.request()
     },
     // 销量排序
     choosesorts: function (e) {
          var that = this
          that.setData({
               goodslist: [],
               num: e.currentTarget.dataset.num,
               imageurld: "/images/kong.png",
               imageurlq: "/images/kong.png",
               jg: 0,
               yhj: 0,
          })
          if (that.data.xl == 0) {
               that.setData({
                    imageurl: "/images/shang.png",
                    xl: 1,
                    sorttype: 5
               })
          } else {
               that.setData({
                    imageurl: "/images/xia.png",
                    xl: 0,
                    sorttype: 6
               })
          }
          that.request()
     },
     // 价格排序
     choosesortd: function (e) {
          var that = this
          that.setData({
               goodslist: [],
               num: e.currentTarget.dataset.num,
               imageurl: "/images/kong.png",
               imageurlq: "/images/kong.png",
               xl: 0,
               yhj: 0,
          })
          if (that.data.jg == 0) {
               that.setData({
                    imageurld: "/images/shang.png",
                    jg: 1,
                    sorttype: 3
               })
          } else {
               that.setData({
                    imageurld: "/images/xia.png",
                    jg: 0,
                    sorttype: 4
               })
          }
          that.request()
     },
     onReachBottom: function () {
          var that = this
          var types = that.data.types
          var pages = that.data.pages
          if (types=="opt"){
               pages++
               that.setData({
                    pages: pages
               })
               that.request()
          }  
     },
     request: function () {
          var that = this
          var id=that.data.id
          var pages = that.data.pages
          var types = that.data.types
          var sorttype = that.data.sorttype
          wx.showLoading({
               title: '加载中...',
          })
          wx.request({
               url: app.data.apiurl + '/ddk/' + types+'/'+id,
               data: {
                    page: pages,
                    sortType: sorttype,
                    pageSize: 20
               },
               header: {
                    'content-type': 'application/json' // 默认值
               },
               method: 'GET',
               dataType: 'json',
               success: function (res) {
                    wx.hideLoading()
                    var rec=that.data.goodslist
                    var goodslist = res.data.data.goods_list
                    goodslist.forEach(function (item, index, array) {
                         if (((item.min_normal_price / 100).toString()).indexOf(".") == "-1") {
                              array[index] = {
                                   img: item.goods_thumbnail_url,
                                   name: item.goods_name,
                                   id: item.goods_id,
                                   group: (item.min_group_price - item.coupon_discount) / 100,
                                   normal: item.min_group_price / 100,
                                   discount: item.coupon_discount / 100,
                                   quantity: item.sold_quantity
                              }
                         } else {
                              var nun = (item.min_normal_price / 100).toString().split(".")[1].length
                              array[index] = {
                                   img: item.goods_thumbnail_url,
                                   name: item.goods_name,
                                   id: item.goods_id,
                                   group: ((item.min_group_price - item.coupon_discount) / 100).toFixed(nun),
                                   normal: item.min_group_price / 100,
                                   discount: item.coupon_discount / 100,
                                   quantity: item.sold_quantity
                              }
                         } 
                    });
                    for (var i = 0; i < goodslist.length; i++) {
                         rec.push(goodslist[i]);
                    }
                    that.setData({
                         goodslist: rec
                    })
               },
               fail: function (res) {
                    wx.showToast({
                         title: '网络异常',
                         icon: 'none',
                         duration: 2000
                    })
               }
          })
     },
     /**
      * 用户点击右上角分享
      */
     onShareAppMessage: function (res) {
          var that = this
          if (res.from === 'button') {
               // 来自页面内转发按钮
               //console.log(res.target)
          }
          return {
               title: "商品",
               path: '/pages/theme/theme?types='+that.data.types+ '&id=' +that.data.id,
               success: function (res) {
                    //console.log(res)
                    wx.showToast({
                         title: '发送成功',
                         icon: 'none'
                    })
               },
               fail: function (res) {
                    // 转发失败
                    wx.showToast({
                         title: '发送失败',
                         icon: 'none'
                    })
               }
          }
     }
})